package com.字母异位词分组;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> hm = new HashMap<String,List<String>>();
        for(int i =0;i<strs.length;i++){
            String s = strs[i];
            char[] temp = s.toCharArray();
            Arrays.sort(temp);
            s = new String(temp);
            if(hm.containsKey(s)){
                hm.get(s).add(strs[i]);
            }
            else{
                hm.put(s,new ArrayList<String>());
                hm.get(s).add(strs[i]);
            }
        }
        List<List<String>> result = new ArrayList<List<String>>();
        Set<String> keys = hm.keySet();
        for(String s :keys){
            result.add(hm.get(s));
        }
        return result;
    }
}
